<template>
  <div class="dialog-mask" v-if="show">
    <div class="dialog">
      <div class="dialog-header">
        <div class="movie-name" :title="movieTitle">{{ movieTitle }}</div>
        <button class="close" @click="$emit('close')" aria-label="关闭">×</button>
      </div>
      <div class="dialog-title">为该电影评分</div>
      <div class="stars">
        <span v-for="n in 5" :key="n" class="star"
              :class="{active: n <= score}" @click="score = n">★</span>
      </div>
      <div class="dialog-actions">
        <button class="btn" @click="onSubmit">提交</button>
        <button class="btn cancel" @click="$emit('close')">取消</button>
      </div>
      <div v-if="error" class="error">{{ error }}</div>
    </div>
  </div>
</template>
<script setup>
/* eslint-env vue/setup-compiler-macros */
/* eslint-disable no-unused-vars */
import { ref, watch } from 'vue';
const props = defineProps({
  show: { type: Boolean, default: false },
  movieTitle: { type: String, default: '' },
  // 预填的初始分数（用于“修改评分”场景）
  initialScore: { type: [Number, String], default: 0 }
});
const emit = defineEmits(['submit', 'close']);
const score = ref(5);
const error = ref('');

function clampScore(v) {
  const n = Math.round(Number(v) || 0)
  if (!Number.isFinite(n) || n <= 0) return 5
  return Math.max(1, Math.min(5, n))
}

watch(() => props.show, (val) => {
  if (val) {
    // 打开时使用传入的初始分数；无则默认 5 星
    score.value = clampScore(props.initialScore);
    error.value = '';
  }
});

function onSubmit() {
  if (score.value < 1) {
    error.value = '请选择评分';
    return;
  }
  emit('submit', score.value);
}
</script>
<style scoped>
.dialog-mask {
  position: fixed; left: 0; top: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.18);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
}
.dialog {
  background: #fff;
  border-radius: 12px;
  padding: 32px 28px 20px 28px;
  min-width: 320px;
  box-shadow: 0 4px 32px rgba(0,0,0,0.10);
  text-align: center;
}
.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.movie-name {
  font-size: 1.05rem;
  font-weight: 700;
  color: #222;
  max-width: 420px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.close {
  background: transparent;
  border: none;
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  color: #777;
}
.close:hover { color: #333; }
.dialog-title {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 18px;
}
.stars {
  font-size: 2rem;
  margin-bottom: 18px;
}
.star {
  cursor: pointer;
  color: #e0e0e0;
  transition: color 0.2s;
}
.star.active {
  color: #ffb300;
}
.dialog-actions {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin-bottom: 8px;
}
.btn {
  background: #1976d2;
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 7px 18px;
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s;
}
.btn.cancel {
  background: #e0e0e0;
  color: #333;
}
.error {
  color: #d32f2f;
  margin-top: 8px;
  font-size: 0.95rem;
}
</style>